/**
 * Created by szc on 2016/12/29.
 */

class NumPanel extends egret.DisplayObjectContainer {
    private interval = 10;
    // 设计将每个数字的长高
    private numWidth:number;
    private numHeight:number;
    private num:number;
    private style:string;
    private group= new egret.DisplayObjectContainer();
    public constructor(numWidth:number,numHeight:number,style:string) {
        super();
        this.numWidth = numWidth;
        this.numHeight = numHeight;
        this.style = style;
        this.addEventListener(egret.Event.ADDED_TO_STAGE, this.onAddToStage, this);
    }
    private onAddToStage(event: egret.Event) {
        //this.draw();
    }

    /**
     * 获取数
     * @returns {number}
     */
    public getNum():any{
        return this.num;
    }

    /**
     * 设置数
     * @param num
     */
    public setNum(num:any):void{
         this.num = num;
         this.draw();

    }
    /**
     * 绘制当前的面板
     */
    private draw():void{

        this.closeDrawPanle();

        var numArr  = this.num.toString();
        for(var i = 0;i<numArr.length;i++) {
        // for(var i = numArr.length-1;i>-1;i--) {
            var img = this.getOneNum(numArr[numArr.length-i-1]);
            img.x = -(i*this.numWidth -5);
            this.group.addChild(img);
            // console.log(i,numArr[i]);
        }
        this.addChild(this.group);
    }

    /**
     * 获取创建单个数字的指针
     * @param num
     * @returns {ImageBitmap}
     */
    private  getOneNum(num:any):any{
        var imgCon = [];
        imgCon['url'] = this.style+num+"_png";
        imgCon['width'] = this.numWidth;
        imgCon['height'] = this.numHeight;
        var img = new ImageBitmap(imgCon);
        return img.getImgPoint();
    }

    private closeDrawPanle(){
        if(this.contains(this.group)){
            this.removeChild(this.group);
        }
        this.group = new egret.DisplayObjectContainer();
    }


}